package com.zhangyj.util;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AlgorithmUtil {
	public static byte[] fieldDesKey = {0x71, (byte)0xc3, 0x53, 0x4B, 0x2A, (byte)0xFE, 0x0F, 0x77};
	private static final String DES_Algorithm = "DES";
	private static Cipher desCipher;
	private static java.security.MessageDigest mdigest;

	static {
		try {
			desCipher = Cipher.getInstance(DES_Algorithm);
			mdigest = java.security.MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
	}

	public static final byte[] desEncrypt(byte[] bytes, byte[] key) {
		try {
			SecretKey skey = new SecretKeySpec(key, DES_Algorithm);
			desCipher.init(javax.crypto.Cipher.ENCRYPT_MODE, skey);
			return desCipher.doFinal(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static final byte[] desDecrypt(byte[] bytes, byte[] key) {
		try {
			SecretKey skey = new SecretKeySpec(key, DES_Algorithm);
			desCipher.init(javax.crypto.Cipher.DECRYPT_MODE, skey);
			return desCipher.doFinal(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static final String desEncryptToString(byte[] bytes, byte[] key) {
		return bytesToString(desEncrypt(bytes, key));
	}

	public static final byte[] desDecrypt(String text, byte[] key) {
		return desDecrypt(stringToBytes(text), key);
	}

	public static final String desDecryptToString(String text, byte[] key) {
		return bytesToString(desEncrypt(stringToBytes(text), key));
	}

	public static final String bytesToString(byte bytes[]) {
		StringBuilder result = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			int value = bytes[i];
			if (value < 0)
				value += 256;
			String tempStr = Integer.toHexString(value);
			if (tempStr.length() == 1)
				result.append('0');
			result.append(tempStr);
		}

		return result.toString();
	}

	public static final byte[] stringToBytes(String text) {
		int len = text.length();
		byte[] result = new byte[len / 2];
		for (int i = 0; i < len / 2; i++)
			result[i] = (byte) Integer.parseInt(text
					.substring(i * 2, i * 2 + 2), 16);
		return result;
	}

	/**
	 * MD5 摘要计算(String).
	 * 
	 * @param src
	 *            String
	 * @throws Exception
	 * @return String
	 */
	public static String md5Digest(byte[] src) throws Exception {
		return bufferToHex(mdigest.digest(src));
	}

	private static String bufferToHex(byte bytes[], int m, int n) {
		StringBuilder stringBuilder = new StringBuilder(2 * n);
		int k = m + n;
		for (int l = m; l < k; l++) {
			appendHexPair(bytes[l], stringBuilder);
		}
		return stringBuilder.toString();
	}

	private static void appendHexPair(byte bt, StringBuilder stringbuffer) {
		char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换, >>>
		// 为逻辑右移，将符号位一起右移,此处未发现两种符号有何不同
		char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换
		stringbuffer.append(c0);
		stringbuffer.append(c1);
	}

	protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	private static String bufferToHex(byte bytes[]) {
		return bufferToHex(bytes, 0, bytes.length);
	}
	public static void main(String[] args) {
		String password = "z";
		byte[] fieldDesKey = {0x71, (byte)0xc3, 0x53, 0x4B, 0x2A, (byte)0xFE, 0x0F, 0x77};
		try {
			password = AlgorithmUtil.md5Digest(AlgorithmUtil.desEncrypt(password.getBytes(), fieldDesKey));
			System.out.println(password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
